using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class VPatternDetectorDebugger : MonoBehaviour {
	
	public Texture touchTexture;
	public Texture patternTexture;
	public Texture patternTexture1;
	public Texture patternTexture2;
	public Texture patternTexture3;
	public Texture coneTexture;
	
	private int guiScale;
	private Matrix4x4 guiMatrix;
	public GUIStyle nameStyle; 
	public string namePerso; 
	public VPattern pat;
	
	void Awake()
	{
		guiScale = Screen.height / 1024;
		if(guiScale == 0) {
			guiScale=1;
		}
		guiMatrix = Matrix4x4.Scale(new Vector3(guiScale, guiScale, 1));
		print("guiScale"+guiScale);
	}

	void Start () 
	{
		VPatternDetector.SetDistanceTolerance(2.0f);

		if(!VPatternDetector.RegisterPattern(23, new Vector2(0.0f, 12.5f), 3, new Vector2(0, 7)))print ("Can't register pattern 3");

		if(!VPatternDetector.RegisterPattern(15, new Vector2(0, 21.5f), 4, new Vector2(0, 10)))print ("Can't register pattern 4");

		if(!VPatternDetector.RegisterPattern(22, new Vector2(0.0f, 15.75f), 5, new Vector2(0, 7)))print ("Can't register pattern 5");
	}
	
		
	void OnGUI()
	{		
		GUIUtility.ScaleAroundPivot(new Vector2(guiScale, guiScale), new Vector2(Screen.width/2,Screen.height/2)); 
		
		
		GUI.matrix = guiMatrix;
		
		if (!VPatternDetector.IsSupported()) {
			GUI.Label(new Rect(20, 20, 600, 40), "pattern detection not supported on this platform", nameStyle);
		}
		else {
			// display PPI
			//GUI.Label(new Rect(20, 60, 600, 40), "unity ppi="+Screen.dpi+" width="+Screen.width + "height="+Screen.height, nameStyle);
		}
				
		// Detected patterns
		int patCount = VPatternDetector.DetectPatterns();
		
		// display touches
		int screenHeight 	= Screen.height/guiScale;
		int touchWidth		= touchTexture.width;
		int touchHeight		= touchTexture.height;
		int touchCount		= Input.touchCount;
		
		for (int i = 0; i < touchCount; ++i) {
			Touch touch = Input.GetTouch(i);
			if (touch.phase!=TouchPhase.Ended && touch.phase!=TouchPhase.Canceled) {
				Vector2 pos = new Vector2(touch.position.x/guiScale, touch.position.y/guiScale);
				pos.y = screenHeight - pos.y;
				
				// real position
				GUI.DrawTexture(new Rect(pos.x - touchWidth/2, pos.y - touchHeight/2, touchWidth, touchHeight), touchTexture);
			}
		}
	
		
		// display detected patterns
		int patWidth		= patternTexture.width;
		int patHeight		= patternTexture.height;
		int coneWidth		= coneTexture.width;
		int coneHeight		= coneTexture.height;
		Matrix4x4 matrixBackup;
		Vector2 dirRef		= new Vector2(0, 1);
		
		for (int patIndex = 0; patIndex < patCount; ++patIndex) {
						pat = VPatternDetector.GetDetectedPattern (patIndex);
						Vector2 pos = new Vector2 (pat.position.x / guiScale, pat.position.y / guiScale);
						pos.y = screenHeight - pos.y;
			
						Vector2 dir = pat.direction;
						float angle = AngleSigned (dir, dirRef);
			
						// real position
						// pattern

						switch (pat.userData) {
						
						case 3 : 
							namePerso = "Vulco";
							patternTexture = patternTexture1;
							break;
							
						case 4 : 
							namePerso = "Heriw";
							patternTexture = patternTexture2;
							break;
							
						case 5 :
							namePerso = "Tsuna";
							patternTexture = patternTexture3;
							break;
							
						}
						GUI.DrawTexture (new Rect (pos.x - patWidth / 2, pos.y - patHeight / 2, patWidth, patHeight), patternTexture);
			
						// cone
						matrixBackup = GUI.matrix;
						GUIUtility.RotateAroundPivot (angle, new Vector2 (pos.x * guiScale, pos.y * guiScale));	
						GUI.DrawTexture (new Rect (pos.x - coneWidth / 2, pos.y - coneHeight, coneWidth, coneHeight), coneTexture);
						GUI.matrix = matrixBackup;
			
						// pattern
						GUI.DrawTexture (new Rect (pos.x - patWidth / 2, pos.y - patHeight / 2, patWidth, patHeight), patternTexture);
			
						// cone
						matrixBackup = GUI.matrix;
						GUIUtility.RotateAroundPivot (angle, new Vector2 (pos.x * guiScale, pos.y * guiScale));	
						GUI.DrawTexture (new Rect (pos.x - coneWidth / 2, pos.y - coneHeight, coneWidth, coneHeight), coneTexture);
						GUI.matrix = matrixBackup;

			switch (pat.userData) {

				case 3 : 
				namePerso = "Vulco";
					break;

				case 4 : 
				namePerso = "Heriw";
					break;

				case 5 :
				namePerso = "Tsuna";
					break;
			
			}

			// label
			GUI.Label(new Rect(pos.x+200, pos.y-50, 200, 50), namePerso, nameStyle);
	

		}
	}
	
	float AngleSigned(Vector2 v1, Vector2 v2) 
	{
		v1.Normalize();
		v2.Normalize();
		float angle = Mathf.Atan2(v1.x * v2.y - v1.y * v2.x, Vector2.Dot(v1, v2));
		if (Mathf.Abs(angle) < Mathf.Epsilon) {
			return 0;
		}
		return angle * Mathf.Rad2Deg;	
	}	
}
